package com.djk.daily.thread.threadobjectclasscommonmethods;

/**
 * @Description 3个线程，线程1和线程2首先被阻塞，线程3唤醒它们。notify, notifyAll。 start先执行不代表线程先启动。
 * @Author dujunkai
 * @Date 2021/7/3 18:19
 **/
public class WaitNotifyAll implements Runnable {
    private static final Object resource = new Object();

    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = new WaitNotifyAll();
        Thread threadA = new Thread(runnable);
        Thread threadB = new Thread(runnable);
        Thread threadC = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (resource){
//                    resource.notify();
                    resource.notifyAll();
                    System.out.println("线程：" + Thread.currentThread().getName() + "执行notify/notifyAll");
                }
            }
        });
        threadA.start();
        threadB.start();
        Thread.sleep(200);
        threadC.start();

    }

    @Override
    public void run() {
        synchronized (resource){
            System.out.println("线程：" + Thread.currentThread().getName() + "获取到了锁");
            try {
                System.out.println("线程：" + Thread.currentThread().getName() + "执行wait(),即将释放锁");
                resource.wait();
                System.out.println("线程：" + Thread.currentThread().getName() + "执行结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
